package com.mjf.cjt.mapper;

import com.mjf.cjt.domain.PermanentAuthEntity;
import com.mjf.cjt.domain.TicketEntity;
import com.mjf.cjt.domain.TokenEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface CjtAuthMapper {

    /**
     *    <select id="getToken" resultType="com.ld.transfer.common.cjt.entity.TokenEntity">
     *         select id ,access_token accessToken,refresh_token refreshToken,expires,date from t_cjt_token
     *     </select>
     *     <insert id="saveToken" parameterType="com.ld.transfer.common.cjt.entity.TokenEntity">
     *         insert into t_cjt_token(access_token,refresh_token,expires,date,org_id) values(#{accessToken},#{refreshToken},#{expires},CURRENT_DATE(),#{orgId}) ON DUPLICATE KEY UPDATE org_id = #{orgId}
     *     </insert>
     *
     *     <select id="getTicket" parameterType="java.lang.String" resultType="com.ld.transfer.common.cjt.entity.TicketEntity">
     *       select app_key appKey,ticket from t_cjt_ticket where app_key = #{app_key}
     *     </select>
     *     <insert id="saveTicket" parameterType="com.ld.transfer.common.cjt.entity.TicketEntity">
     *       insert into t_cjt_ticket(app_key,ticket,create_time) values(#{appKey},#{ticket},CURRENT_DATE()) ON DUPLICATE KEY UPDATE ticket = #{ticket},update_time = CURRENT_DATE()
     *     </insert>
     *     <select id="getPermanentAuthCode" parameterType="java.lang.String" resultType="com.ld.transfer.common.cjt.entity.PermanentAuthEntity">
     *         select org_id orgId, permanent_auth_code permanentAuthCode from t_cjt_company_permanent_authcode where org_id = #{orgId}
     *     </select>
     *     <insert id="savePermanentAuthCode" parameterType="com.ld.transfer.common.cjt.entity.PermanentAuthEntity">
     *         insert into t_cjt_company_permanent_authcode(org_id,permanent_auth_code,create_time) values(#{orgId},#{permanentAuthCode},CURRENT_DATE()) ON DUPLICATE KEY UPDATE permanent_auth_code = #{permanentAuthCode},update_time = CURRENT_DATE()
     *     </insert>
     *
     *     <select id="getUserCode" parameterType="java.lang.String" resultType="java.lang.String">
     *         select user_auth_code userAuthCode from t_cjt_user_permanent_authcode where org_id = #{orgId}
     *     </select>
     *     <insert id="saveUserPermanentCode" parameterType="java.lang.String">
     *         insert into t_cjt_user_permanent_authcode(org_id,user_auth_code,create_time) values(#{orgId},#{permanentAuthCode},CURRENT_DATE()) ON DUPLICATE KEY UPDATE user_auth_code = #{permanentAuthCode},update_time = CURRENT_DATE()
     *     </insert>
     * @param appKey
     * @return
     */

    @Select("select app_key appKey,ticket from t_cjt_ticket where app_key = #{appKey}")
    TicketEntity getTicket(@Param("appKey") String appKey);

    @Insert("insert into t_cjt_ticket(app_key,ticket,create_time) values(#{ent.appKey},#{ent.ticket},CURRENT_DATE()) " +
        "ON DUPLICATE KEY UPDATE ticket = #{ent.ticket},update_time = CURRENT_DATE()")
    void saveTicket(@Param("ent") TicketEntity ticketEntity);

    @Select("select org_id orgId, permanent_auth_code permanentAuthCode from t_cjt_company_permanent_authcode where org_id = #{orgId}")
    PermanentAuthEntity getPermanentAuthCode(@Param("orgId") String orgId);

    @Select("insert into t_cjt_company_permanent_authcode(org_id,permanent_auth_code,create_time) " +
        "values(#{ent.orgId},#{ent.permanentAuthCode},CURRENT_DATE()) ON DUPLICATE KEY " +
        "UPDATE permanent_auth_code = #{ent.permanentAuthCode},update_time = CURRENT_DATE()")
    void savePermanentAuthCode(@Param("ent") PermanentAuthEntity permanentAuthEntity);

    @Select("select user_auth_code userAuthCode from t_cjt_user_permanent_authcode where org_id = #{orgId}")
    String getUserCode(@Param("orgId") String orgId);

    /**
     * 通过临时code获取token方式获取用户永久code
     * @param permanentAuthCode
     */
    @Insert("insert into t_cjt_user_permanent_authcode(org_id,user_auth_code,create_time) " +
        "values(#{orgId},#{permanentAuthCode}," +
        "CURRENT_DATE()) ON DUPLICATE KEY UPDATE user_auth_code = #{permanentAuthCode},update_time = CURRENT_DATE()")
    void saveUserPermanentCode(@Param("permanentAuthCode") String permanentAuthCode, @Param("orgId") String orgId);

    @Insert("insert into t_cjt_token(access_token,refresh_token,expires,date,org_id) " +
        "values(#{ent.accessToken},#{ent.refreshToken},#{ent.expires}," +
        "CURRENT_DATE(),#{ent.orgId}) ON DUPLICATE KEY UPDATE org_id = #{ent.orgId}")
    void saveToken(@Param("ent") TokenEntity token);
}
